package com.jcfk.eam.controller.mould;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.business.utils.DataBindHelper;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.jcfk.eam.domain.query.mould.EamMouldOutinboundRecordQuery;
import com.jcfk.eam.domain.dto.mould.EamMouldOutinboundRecordDTO;
import com.jcfk.eam.service.mould.EamMouldOutinboundRecordService;
import com.jcfk.common.core.utils.ExcelUtils;
import com.jcfk.eam.domain.excel.mould.EamMouldOutinboundRecordExcel;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import com.alibaba.excel.EasyExcel;
import org.springframework.web.multipart.MultipartFile;
/**
 * 模具出入库记录表
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2025-08-01
 */
@Api(tags = {"模具出入库记录表管理"})
@RestController
@RequestMapping("eammouldoutinboundrecord")
public class EamMouldOutinboundRecordController {
    @Autowired
    private EamMouldOutinboundRecordService eamMouldOutinboundRecordService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamMouldOutinboundRecordQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询模具出入库记录表")
    @PostMapping("/queryByPage")
    public RetPage<EamMouldOutinboundRecordDTO> queryByPage(@RequestBody ReqPageParam<EamMouldOutinboundRecordQuery> requestParam) {
        DataPage<EamMouldOutinboundRecordDTO> page = new DataPage<EamMouldOutinboundRecordDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamMouldOutinboundRecordDTO.class);
        page = eamMouldOutinboundRecordService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     *  模具出入库记录表
     * 根据id 查询详情
     */
    @ApiOperation("查询模具出入库记录表")
    @GetMapping("/get/{id}")
    public RetResult<EamMouldOutinboundRecordDTO> get(@PathVariable("id") String id) {
            EamMouldOutinboundRecordDTO item = eamMouldOutinboundRecordService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理模具出入库记录表
     */
    @ApiOperation("保存模具出入库记录表")
    @PostMapping("/save")
    @RequestLogHeader(modular = "模具出入库记录表管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamMouldOutinboundRecordDTO eamMouldOutinboundRecord) {
        // 数据校验
        RetResult ret = eamMouldOutinboundRecordService.isValid(eamMouldOutinboundRecord);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamMouldOutinboundRecord.getId())){
            // 新增
            eamMouldOutinboundRecordService.insert(eamMouldOutinboundRecord);
        } else{
            // 更新
            eamMouldOutinboundRecordService.update(eamMouldOutinboundRecord);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除模具出入库记录表
     */
    @ApiOperation("批量删除模具出入库记录表")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "模具出入库记录表管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamMouldOutinboundRecordDTO eamMouldOutinboundRecord) {
        return RetResult.ok(eamMouldOutinboundRecordService.logicDeleteList(eamMouldOutinboundRecord));
    }


    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody EamMouldOutinboundRecordQuery requestParam, HttpServletResponse response) throws Exception {
        List<EamMouldOutinboundRecordDTO> list = eamMouldOutinboundRecordService.getList(requestParam);
        ExcelUtils.exportExcelToTarget(response, null, list, EamMouldOutinboundRecordExcel.class);
    }



    /**
     *  删除模具出入库记录表
     */
    @ApiOperation("删除模具出入库记录表")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "模具出入库记录表管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamMouldOutinboundRecordService.deleteItem(id);
        return RetResult.ok();
    }
}
